#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Button.h"


int ButtonInit(ButtonTypedef *button)
{
    if(button->withPicture)
    {
        char filePath[32];
        if(button == NULL)
        {
            return -1; 
        }
        
        strcpy(filePath, button->prefixPath);
        strcat(filePath, "Off.rgba");
        if(LayerInit(&button->ButtonOffLayerSrc, filePath))
            return -1;

        /* 拨动开关只有开和关的图 */
        if(button->buttonType == TypeSwitch)
        {
            strcpy(filePath, button->prefixPath);
            strcat(filePath, "On.rgba");
            if(LayerInit(&button->ButtonOnLayerSrc, filePath))
                return -1;
        }
        /* 触动按键只有关闭和按下的图 */
        else if(button->buttonType == TypePress)
        {
            strcpy(filePath, button->prefixPath);
            strcat(filePath, "Touching.rgba");
            if(LayerInit(&button->ButtonTouchingSrc, filePath))
                return -1;
        }
    }
    return 0;
}


int ButtonPictureUpdate(ButtonTypedef *button)
{
    struct ak_tde_layer *_layer;

    /* 显示图片 */
    if(button->withPicture)
    {
        /* 更新按键图片 */
        switch (button->status)
        {
        case ButtonOff:
        case ButtonToOn:
            _layer = &(button->ButtonOffLayerSrc);
            break;
        case ButtonOn:
        case ButtonToOff:
            _layer = &(button->ButtonOnLayerSrc);
            break;
        case ButtonTouching:
            _layer = &(button->ButtonTouchingSrc);
            break;
        default:
            break;
        }

        _layer->pos_height = _layer->height;
        _layer->pos_width  = _layer->width;
        if(AddLayer(_layer,\
        button->buttonScale.centerLeft + button->LayerDertaPos.Left,\
        button->buttonScale.centerTop  + button->LayerDertaPos.Top) < 0)
        {
            printf("Add Layer fail!\n");
        }
    }

    /* 显示按键范围 */
    if(DEBUG_MODE)
    {
        FillRect(button->buttonScale.centerLeft, button->buttonScale.centerTop, button->buttonScale.width, button->buttonScale.height);
    }

    /* 显示按键附带的字符串 */
    if(strlen(button->buttonString.string) > 0)
    {
        if(button->status == ButtonOff || button->status == ButtonToOn)
            button->buttonString.stringRGB = button->stirngColor.color_off;
        else
            button->buttonString.stringRGB = button->stirngColor.color_on;
        switchString2Rgba(&button->buttonString);
        // printf("strlen %d\n", strlen(button->buttonString.string));
        AddLayer(&button->buttonString.srcLayer,\
                button->buttonScale.centerLeft + button->stringDertaPos.Left,\
                button->buttonScale.centerTop  + button->stringDertaPos.Top);
    }
}


int ButtonTouchCheck(ButtonTypedef *button, struct ak_ts_event *ts_event)
{
    // printf("x:%d, y:%d\n", touchPosition.left, touchPosition.top);
    int i;
    PosTypedef switchPos;       //触摸屏坐标转换为显示屏坐标
    bool pressed = 0;

    /* 确定按键是否被按下 */
    for(i=0;i<TS_POINT_MAX_NUM;i++)
    {
        if(ts_event->map & 0x01<<i)
        {
            switchPos.Left = 1024 - ts_event->info[i].x;
            switchPos.Top  = ts_event->info[i].y;
            if(abs(switchPos.Top - button->buttonScale.centerTop) < button->buttonScale.height/2\
            && abs(switchPos.Left- button->buttonScale.centerLeft)< button->buttonScale.width/2)
            {
                pressed = 1;
                ts_event->map &= ~(0x01<<i);         //消掉某个按键，避免一次点击同时触发重叠的按键
                button->touchDertaPos.Left = switchPos.Left- button->buttonScale.centerLeft;
                button->touchDertaPos.Top = switchPos.Top - button->buttonScale.centerTop;
            }
            // printf("pnt%d, x%d, y%d\n", i, switchPos.Left, switchPos.Top);
        }
    }

    /* 给出按键的转化后的状态 */
    if(pressed)
    {
        if(button->buttonType == TypeSwitch)
        {
            if(button->status == ButtonOff)
            {
                button->status = ButtonToOn;
                return 1;
            }
            else if(button->status == ButtonOn)
            {
                button->status = ButtonToOff;
                return 1;
            }
            else{}
        }
        else if(button->buttonType == TypePress)
        {
            button->status = ButtonTouching;
            return 1;
        }
        else{}
    }
    else
    {
        if(button->buttonType == TypeSwitch)
        {
            if(button->status == ButtonToOn)
            {
                button->status = ButtonOn;
                return 1;
            }
            else if(button->status == ButtonToOff)
            {
                button->status = ButtonOff;
                return 1;
            }
            else{}
        }
        else if(button->buttonType == TypePress)
        {
            if(button->status == ButtonTouching)
            {
                button->status = ButtonOff;
                return 1;
            }
            else{}
        }
        else{}
    }
    return 0;
}
